{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7d5afc1a",
   "metadata": {},
   "source": [
    "## 安装\n",
    "\n",
    "pip install redis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "045c1b3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import redis \n",
    "import json\n",
    "\n",
    "redisCfg = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\rediscfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    redisCfg = cfg\n",
    "    # r = redis.Redis(host='192.168.31.196', port=6379)  # 无密码 \n",
    "    # r = redis.Redis(host=redisCfg['host'], port=redisCfg['port'], password=redisCfg['passwd']) # 单个连接\n",
    "    pool = redis.ConnectionPool(host=redisCfg['host'], port=redisCfg['port'], password=redisCfg['passwd'], decode_responses=True, encoding='UTF-8') #连接池\n",
    "    r = redis.Redis(connection_pool=pool)\n",
    "    r.set('name', '李磊')\n",
    "    print(r.get('name'))\n",
    "    r.close\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeae1e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "import redis \n",
    "import json\n",
    "\n",
    "redisCfg = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\rediscfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    redisCfg = cfg\n",
    "    pool = redis.ConnectionPool(host=redisCfg['host'], port=redisCfg['port'], password=redisCfg['passwd'], decode_responses=True, encoding='UTF-8') #连接池\n",
    "    r = redis.Redis(connection_pool=pool)\n",
    "    r.setex('name', 10,  '李磊') # 有效时间10秒  # px毫秒 \n",
    "    print(r.get('name'))\n",
    "    r.close\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5356b9ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import redis \n",
    "import json\n",
    "\n",
    "redisCfg = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\rediscfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    redisCfg = cfg\n",
    "\n",
    "pool = redis.ConnectionPool(host=redisCfg['host'], port=redisCfg['port'], password=redisCfg['passwd'], decode_responses=True, encoding='UTF-8') #连接池\n",
    "r = redis.Redis(connection_pool=pool)\n",
    "r.mset({ \"name\": '李磊1', \"name1\":\"韩梅梅\"})\n",
    "print(r.mget('name1', 'name'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52a661bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "None\n",
      "list\n"
     ]
    }
   ],
   "source": [
    "import redis \n",
    "import json\n",
    "\n",
    "redisCfg = {}\n",
    "\n",
    "with open(\"E:\\\\projs\\\\my\\\\rediscfg.json\",'r') as f:\n",
    "    cfg = json.load(f)\n",
    "    redisCfg = cfg\n",
    "\n",
    "pool = redis.ConnectionPool(host=redisCfg['host'], port=redisCfg['port'], password=redisCfg['passwd'], decode_responses=True, encoding='UTF-8') #连接池\n",
    "r = redis.Redis(connection_pool=pool)\n",
    "\n",
    "r.lpush(\"list\", 1)\n",
    "print(r.llen(\"list\"))\n",
    "print(r.lindex(\"list\", 20))\n",
    "print(r.type(\"list\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02c2e33b",
   "metadata": {},
   "source": [
    "## 方法\n",
    "\n",
    "### 通用的操作\n",
    "\n",
    "- delete(*names)，根据name删除redis中的任意数据类型\n",
    "- exists(name)，检测redis的name是否存在\n",
    "- keys(pattern='*')，根据* ？等通配符匹配获取redis的name\n",
    "- expire(name ,time)，为某个name设置超时时间\n",
    "- rename(src, dst)，重命名\n",
    "- move(name, db)，将redis的某个值移动到指定的db下\n",
    "- randomkey()，随机获取一个redis的name（不删除）\n",
    "- type(key)，获取key对应值的类型\n",
    "\n",
    "\n",
    "### 字符串\n",
    "\n",
    "- getrange(key, start, end) # 读取截取后的字符串\n",
    "- append(key, appendValue) # 追加并设置值\n",
    "- strlen(key) 一个汉子可能对应三个字节\n",
    "\n",
    "### 列表\n",
    "\n",
    "- lpush\n",
    "- rpush\n",
    "- llen\n",
    "- linsert\n",
    "- lset\n",
    "- lpop\n",
    "- lindex\n",
    "- lrange 切片取出\n",
    "- ltrim 移除 start end外的值\n",
    "\n",
    "### 哈希\n",
    "\n",
    "- hset(name, key, value)\n",
    "- hgetall(name) 返回 { 'a1': 'a2'}\n",
    "- hmset\n",
    "- hmget\n",
    "- hlen\n",
    "- hkeys\n",
    "- hvals\n",
    "- hexists\n",
    "- hdel\n",
    "\n",
    "### Set\n",
    "\n",
    "- sadd\n",
    "- smembers  获取集合中所有的成员\n",
    "- scard 获取key对应的集合的元素的个数\n",
    "- sdiff\n",
    "- sdiffstore\n",
    "- sinter\n",
    "- sinterstore\n",
    "- smove\n",
    "- spop 右侧移除\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
